package com.android.permission;

import android.content.Context;
import android.text.TextUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @ClassName: SystemProperty
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-05-28
 */
public class SystemProperty {

    private final Context mContext;

    public SystemProperty(Context mContext) {
        this.mContext = mContext;
    }

    public String getOrThrow(String key) throws NoSuchPropertyException {
        try {
            ClassLoader classLoader = mContext.getClassLoader();
            Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
            Method methodGet = SystemProperties.getMethod("get", String.class);
            return (String) methodGet.invoke(SystemProperties, key);
        } catch (ClassNotFoundException e) {
            throw new NoSuchPropertyException(e);
        } catch (NoSuchMethodException e) {
            throw new NoSuchPropertyException(e);
        } catch (InvocationTargetException e) {
            throw new NoSuchPropertyException(e);
        } catch (IllegalAccessException e) {
            throw new NoSuchPropertyException(e);
        }
    }

    public String get(String key) {
        try {
            return getOrThrow(key);
        } catch (NoSuchPropertyException e) {
            return null;
        }
    }

    public boolean containsKey(final String key) {
        String value;
        try {
            value = getOrThrow(key);
        } catch (NoSuchPropertyException e) {
            return false;
        }

        if (TextUtils.isEmpty(value)){
            return false;
        }else {
            return true;
        }
    }
}
